package com.alvinlkk.basic.chapter21;

import java.util.concurrent.TimeUnit;

//通过内部类将线程代码隐藏
public class ThreadVariations {
    public static void main(String[] args) {
        new InnerThread1("InnerThread1");
        new InnerThread2("InnerThread2");
        new InnerRunner1("InnerRunner1");
        new InnerRunner2("InnerRunner2");
        new ThreadMethod("ThreadMethod").runTask();
    }
}

class InnerThread1 {
    private int countDown = 5;
    
    private Inner inner;
    
    private class Inner extends Thread {
        
        public Inner(String name) {
            super(name);
            start();
        }
        
        @Override
        public String toString() {
            return getName() + ": " + countDown;
        }
        
        @Override
        public void run() {
            while (true) {
                System.out.println(this);
                if(-- countDown == 0) return ;
                try {
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public InnerThread1(String name) {
        inner = new Inner(name);
    }
}

class InnerThread2 {
    private int countDown = 5;
    
    private Thread thread;
    
    public InnerThread2(String name) {
        thread = new Thread(name) {
            @Override
            public String toString() {
                return getName() + ": " + countDown;
            }
            
            @Override
            public void run() {
                while (true) {
                    System.out.println(this);
                    if(-- countDown == 0) return ;
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
    }
}
    
class InnerRunner1 {
    private int countDown = 5;
    
    private Inner inner;
    
    private class Inner implements Runnable {
        
        Thread thread;
        
        public Inner(String name) {
            thread = new Thread(this, name);
            thread.start();
        }
        
        @Override
        public String toString() {
            return thread.getName() + ": " + countDown;
        }
        
        @Override
        public void run() {
            while (true) {
                System.out.println(this);
                if(-- countDown == 0) return ;
                try {
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public InnerRunner1(String name) {
        inner = new Inner(name);
    }
}
    
class InnerRunner2 {
    private int countDown = 5;
    
    private Thread thread;
    
    public InnerRunner2(String name) {
        thread = new Thread(new Runnable() {
            
            @Override
            public void run() {
                while (true) {
                    System.out.println(this);
                    if(-- countDown == 0) return ;
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            @Override
            public String toString() {
                return Thread.currentThread().getName() + ": " + countDown;
            }
        }, name);
        
        thread.start();
    }
}
    
class ThreadMethod {
    private int countDown = 5;
    
    private String name;
    
    private Thread thread;
    
    public ThreadMethod(String name) {
        this.name = name;
    }
    
    public void runTask() {
        if(thread == null) {
            thread = new Thread(name) {
                @Override
                public String toString() {
                    return getName() + ": " + countDown;
                }
                
                @Override
                public void run() {
                    while (true) {
                        System.out.println(this);
                        if(-- countDown == 0) return ;
                        try {
                            TimeUnit.MILLISECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            
            };
            thread.start();
        }
    }
}
